<template>
  <div class="baseTable h100" ref="seleteTableBox">
    <el-table
      :data="tableData"
      border
      stripe
      highlight-current-row
      :height="tableConfig.autoHeight?null:tableConfig.height||tableConfig.maxHeight||400"
      ref="gridData"
      @cell-dblclick="clickRow"
      :row-style="rowClass"
      @selection-change="handleSelectionChange"
      row-key="id"
    >
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <el-table-column
        v-if="tableConfig.indexShow || false"
        label="序号"
        type="index"
        width="55"
        fixed
      ></el-table-column>
      <template v-for="(item,index) in tableConfig.column">
        <el-table-column
          :label="item.label"
          :align="item.textAlign?item.textAlign:null"
          :width="item.width"
          :prop="item.prop"
          :key="index"
          :fixed="item.fixed||null"
        >
          <template slot-scope="scope">
            <span
              class="text-pre-wrap"
            >{{ item.formatter?item.formatter(scope.row):formatterNull(scope.row[scope.column.property])}}</span>
          </template>
        </el-table-column>
      </template>
    </el-table>
  </div>
</template>
<script>
export default {
  props: {
    tableSetting: {
      type: Object,
      default: () => {
        return {};
      },
    },
    hadData: {
      type: Array,
      default: () => {
        return [];
      },
    },
    tableKey: {
      type: String,
      default: "id",
    },
  },
  data() {
    return {
      tableConfig: {
        autoHeight: false,
        column: [],
      },
      tableData: [],
      selectData: [],
      selectRowIndex: [],
    };
  },
  watch: {
    selectData(data) {
      this.selectRowIndex = [];
      data.map((i, index) => {
        this.selectRowIndex.push(i.rowIndex);
      });
    },
  },
  created() {
    this.init();
  },
  mounted() {
    this.checkSeleteData();
    this.setTableHeight();
  },
  methods: {
    init() {
      let { column, data, maxHeight, autoHeight } = this.tableSetting;
      data.map((i, index) => (i.rowIndex = index));

      this.tableConfig = Object.assign({}, { column, maxHeight, autoHeight });
      this.$nextTick(() => {
        this.tableData = data;
      });
    },
    rowClass({ row, rowIndex }) {
      if (this.selectRowIndex.includes(rowIndex)) {
        return {
          "background-color": "#c5e0ff",
        };
      }
    },
    handleSelectionChange(data) {
      this.selectData = data;
    },
    //设置表单数据
    setTableData(data) {
      data.map((i, index) => (i.rowIndex = index));
      this.tableData = JSON.parse(JSON.stringify(data));
      this.checkSeleteData();
    },
    //检查行数据并默认选中
    async checkSeleteData() {
      let res = await this.$nextTick();
      const element = this.$refs.gridData.$el;
      //取交集
      let arr = res.tableData.filter((i) =>
        res.hadData.some((j) => i[this.tableKey] == j[this.tableKey])
      );

      arr.map((i) => {
        const iconElement = element
          .getElementsByClassName("el-table__row")
          [i.rowIndex].getElementsByClassName("el-checkbox__inner");
        if (iconElement.length) {
          iconElement[0].click();
        }
      });
    },
    //点击行
    clickRow(row, column) {
      let columnName = column.property;
      if (columnName !== "check") {
        const element = this.$refs.gridData.$el;
        const iconElement = element
          .getElementsByClassName("el-table__row")
          [row.rowIndex].getElementsByClassName("el-checkbox__inner");
        if (iconElement.length) {
          iconElement[0].click();
        }
      }
    },
    //获取选中的table
    getSeleteData() {
      let arr = this.$refs.gridData.selection;
      return JSON.parse(JSON.stringify(arr));
    },
    //获取table数据
    getTableData() {
      let arr = JSON.parse(JSON.stringify(this.tableData));
      return arr;
    },
    /**判断空值默认- */
    formatterNull(text) {
      if (text === null || text === undefined || text === "") {
        return "-";
      } else {
        return text;
      }
    },
    /**设置最大高度 */
    setTableHeight() {
      let { height, maxHeight, autoHeight } = this.tableSetting;
      if (!height && !maxHeight && !autoHeight) {
        let parentH = this.$refs.seleteTableBox.parentElement.clientHeight;
        parentH = parentH - 36;
        this.tableConfig.height = parentH < 200 ? 200 : parentH;
      }
    },
  },
};
</script>
<style scoped>
</style>
